//
//  SGHttpTool.m
//  ThatOne
//
//  Created by 赵士新 on 15/9/18.
//
//

#import "SGHttpTool.h"
#import "AFNetworking.h"

@implementation SGHttpTool

+(void)postWithURL:(NSString*)url params:(NSDictionary*)params success:(HttpSuccess)success failure:(HttpFailure)failure
{
    AFHTTPRequestOperationManager* operationManager = [AFHTTPRequestOperationManager manager];
    operationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    operationManager.requestSerializer = [AFHTTPRequestSerializer serializer];
    operationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/json",@"text/plain",@"application/json",@"text/html",nil];
    [operationManager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            id json = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            success(json);
            NSLog(@"%@",json);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
            NSLog(@"%@",[error description]);
        }
    }];
}

+(void)getWithURL:(NSString*)url params:(NSDictionary*)params success:(HttpSuccess)success failure:(HttpFailure)failure
{
    AFHTTPRequestOperationManager* operationManager = [AFHTTPRequestOperationManager manager];
    operationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    operationManager.requestSerializer = [AFHTTPRequestSerializer serializer];
    operationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/json",@"text/plain",@"application/json",@"text/html",nil];
    [operationManager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            id json = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",json);
            success(json);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
             NSLog(@"%@",[error description]);
        }
    }];
}


@end
